import axios from "axios";
import { Message } from "element-ui";
// let base_url = '/api'; //测试环境
// create an axios instance
const service = axios.create({
    // baseURL: base_url, // api 的 base_url
    withCredentials: true, // send cookies when cross-domain requests
    timeout: 500000 // request timeout
});
// service.defaults.headers = {
//     'Content-Type': 'application/json;charset=utf-8',
// };
// axios.defaults.withCredentials = true;
// request interceptor
service.interceptors.request.use(
    config => {
        //console.log(store.getters.getUserInfo.accessToken);
        // let accessToken = sessionStorage.getItem('accessToken');
        // if (accessToken) {
        //     // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
        //     // config.headers['authc-token'] = localStorage.getItem('token');
        //     config.headers['Authorization'] = `Bearer ${accessToken}`
        //         // let pm = config.params;
        //         // if(pm == undefined){
        //         //   pm = {};
        //         // }
        //         // pm['token'] = getToken();
        //         // config.params = pm;
        // }
        // alert(1);
        console.log("config", config.method.toLowerCase());
        switch (config.method.toLowerCase()) {
            case 'post':
            case 'put':
                if (config.data) {
                    console.log(toString(config.data.userId));
                    let exp = config.data.userId;
                    if (!exp && typeof(exp) != "undefined" && exp != 0) {
                        window.initLoginDialog(true);
                    }
                }
                break;
            default:
                if (config.params) {
                    let exp2 = config.params.userId;
                    if (!exp2 && typeof(exp2) != "undefined" && exp2 != 0) {
                        window.initLoginDialog(true);
                    }
                }
        }
        //config.headers['Authorization'] = `Basic cGlnOnBpZw==`

        if (config.uploadFile) {
            config.headers['Content-Type'] = 'multipart/form-data'
            let param = new FormData(); // 创建form对象
            param.append("file", config.data.file); // 通过append向form对象添加数据
            config.data = param
        }
        return config;
    },
    error => {
        // Do something with request error
        Promise.reject(error);
    }
);

// response interceptor
service.interceptors.response.use(
    /**
     * 下面的注释为通过在response里，自定义code来标示请求状态
     * 当code返回如下情况则说明权限有问题，登出并返回到登录页
     * 如想通过 xmlhttprequest 来状态码标识 逻辑可写在下面error中
     * 以下代码均为样例，请结合自生需求加以修改，若不需要，则可删除
     */
    response => {
        // debugger;
        const res = response.data;
        // if (res.data === undefined) {
        //     return;
        // }

        // if (res.code != 0) {
        //     Message({
        //         message: res.data.code,
        //         type: "error",
        //         duration: 5 * 1000
        //     });
        //     return Promise.reject("error");
        // }
        return Promise.resolve(res);
    },
    error => {
        return Promise.reject(error);
    }
);

export default service;
